﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Assessment.Management.SharedComponent.Params.CommonParam;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.SharedComponent.Entities;
using System.Data.SqlClient;

namespace Assessment.Management.DAO.CommonManagers
{
    public class OptionHitDAO : BaseDaos
    {
        public OptionHitDAO()
            : base()
        {
        }

        public void GetOptionHitBySubjectIDAndClassID(OptionHitParam param)
        {
            string query = "Select * from OptionHit where OptionID=@OptionID and ClassID=@ClassID and SubjectID=@SubjectID";

            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@OptionID", param.OptionHit.OptionID);
            cmd.Parameters.AddWithValue("@ClassID", param.OptionHit.ClassID);
            cmd.Parameters.AddWithValue("@SubjectID", param.OptionHit.SubjectID);
            try
            {
                param.OptionHit = base.ExecuteQueryGetObject<OptionHit>(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }

        public void GetOptionHitByOptionID(OptionHitParam param)
        {
            string query = "Select * from OptionHit where OptionID=@OptionID";

            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@OptionID", param.OptionHit.OptionID);
            try
            {
                param.OptionHit = base.ExecuteQueryGetObject<OptionHit>(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }

        public void GetListOptionHitByOptionID(OptionHitParam param)
        {
            string query = "Select * from OptionHit where OptionID=@OptionID";

            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@OptionID", param.OptionHit.OptionID);
            try
            {
                param.listOptionHit = base.ExecuteQueryGetListObject<OptionHit>(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }

        public void InsertNewOptionHit(OptionHitParam param)
        {
            string query = @"Insert into OptionHit (OptionID, SubjectID, ClassID, HitCount) 
                                        values(@OptionID, @SubjectID, @ClassID, @HitCount)";

            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@OptionID", param.OptionHit.OptionID);
            cmd.Parameters.AddWithValue("@SubjectID", param.OptionHit.SubjectID);
            cmd.Parameters.AddWithValue("@ClassID", param.OptionHit.ClassID);
            cmd.Parameters.AddWithValue("@HitCount", param.OptionHit.HitCount);

            try
            {
                int count = base.ExecuteNonQuery(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }

        public void UpdateOptionHitCount(OptionHitParam param, SqlCommand sqlCmd = null)
        {
            string query = @"Update OptionHit 
                              Set HitCount       = @HitCount 
                              Where OptionID     = @OptionID and SubjectID = @SubjectID and
                                    ClassID      = @ClassID";

            // if SqlCommand argument is null, it means this function is not in transaction
            bool isUsingTransaction = sqlCmd != null;
            if (sqlCmd == null)
            {
                sqlCmd = base.GetSqlCommand();
            }

            sqlCmd.CommandText = query;
            sqlCmd.Parameters.Clear();
            sqlCmd.Parameters.AddWithValue("@HitCount", param.OptionHit.HitCount);
            sqlCmd.Parameters.AddWithValue("@OptionID", param.OptionHit.OptionID);
            sqlCmd.Parameters.AddWithValue("@SubjectID", param.OptionHit.SubjectID);
            sqlCmd.Parameters.AddWithValue("@ClassID", param.OptionHit.ClassID);

            try
            {
                int count = base.ExecuteNonQuery(sqlCmd);
            }
            finally
            {
                // If this function is using transaction from biz, do not close connection
                if (!isUsingTransaction)
                {
                    base.CloseConnection(sqlCmd);
                }
            }
        }
    }
}
